package boot.spring.controller;

import boot.spring.po.Staff;
import boot.spring.po.User;
import boot.spring.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;


@Controller
public class Login {
    @Autowired
    LoginService loginservice;

    @RequestMapping("/loginvalidate")
    public String loginvalidate(@RequestParam("username") String username,
                                @RequestParam("password") String pwd,
                                HttpSession httpSession) {
        if (username == null)
            return "login";
        // 查询用户
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String realpwd = loginservice.getpwdbyname(username);

        // 用户 非空并且 真实密码不为空 则登录
        if (realpwd != null && pwd.equals(realpwd)) {
            long uid = loginservice.getUidbyname(username);
            httpSession.setAttribute("uid", uid);
            return "chatroom";
        } else {
            return "fail";
        }
    }

    @RequestMapping("toregister")
    public String toregister(@RequestParam("username") String username,
                           @RequestParam("password") String pwd,
                           @RequestParam("password") String email,
                           HttpSession httpSession) {
        // 账号密码非空 注册
        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(pwd)){
            Staff staff =  new Staff();
            staff.setFirst_name(username);
            staff.setEmail(email);
            staff.setUsername(username);
            staff.setPassword(pwd);
            loginservice.insert(staff);
            System.out.println("用户注册成功");
            try {
                Thread.sleep(800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 注册成功登录
            long uid = loginservice.getUidbyname(username);
            httpSession.setAttribute("uid", uid);
            return "chatroom";
        }else {
            return  "register";
        }

    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/register")
    public String register(){return "register";}

    @RequestMapping("/logout")
    public String logout(HttpSession httpSession) {
        return "login";
    }

    @RequestMapping(value = "/currentuser", method = RequestMethod.GET)
    @ResponseBody
    public User currentuser(HttpSession httpSession) {
        Long uid = (Long) httpSession.getAttribute("uid");
        String name = loginservice.getnamebyid(uid);
        return new User(uid, name);
    }
}
